<?php
class SellerAction extends SystemAction {
	public $sellerattrs = array (
			'sendername' => '发件人姓名',
			'sendercom' => '发件人单位',
			'senderaddr' => '发件人地址',
			'sendermobile' => '发件人手机',
			'senderphone' => '发件人电话',
			'sendzip' => '发件人邮编'
	);
	function slist() {
		$count = D ( 'Seller' )->count ();
		import ( "ORG.Util.Page" ); // 导入分页类
		$p = new Page ( $count, $this->pagesize );
		$slist = D ( 'Seller' )->limit ( $p->firstRow . ',' . $p->listRows )->order ( "ID DESC" )->select ();
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign ( "slist", $slist );
		$this->display ();
	}
	/**
	 * 充值
	 */
	function recharge() {
		$id = $_GET ['id'];
		$user = D ( 'Seller' )->where ( "id={$id}" )->find ();
		if (! isset ( $user )) {
			$this->error ( '不存在此用户!' );
		}
		
		$paytype = D ( "Sellersetting" )->getPaytype ( 1 );
		$this->assign ( 'user', $user );
		$this->assign ( 'paytype', $paytype );
		
		$this->display ();
	}
	function rechargesave() {
		$s = D ( "Seller" );
		$p = D ( "Payment" );
		$ss = D ( "Sellersetting" );
		if (! $seller = $ss->getSellerByID ( $_POST ['user_id'] )) {
			$this->error ( '卖家参数错误！' );
		}
		$s->startTrans ();
		$pay = $p->create ();
		if (! $pay) {
			$this->error ( $p->getError () );
		} else {
			$paytype = $ss->getPaytypeOne ( $_POST ['paytypeid'] );
			if (! $paytype) {
				$this->error ( '款项参数错误！' );
			}
			
			$pay ['paycode'] = $ss->createPayCode ();
			$pay ['nick'] = $seller ['nick'];
			$pay ['paynick'] = $_SESSION['seller_nick'];
			$pay ['paytype'] = $paytype ['paytype'];
			$pay ['paymoneyresult'] = intval ( $pay ['paytype'] . '1' * $pay ['paymoney'] );
			$pay ['paydate'] = date ( 'Y-m-d H:i:s' );
			
			$seller ['paybalance'] = $seller ['paybalance'] + $pay ['paymoneyresult'];
			if ($seller ['paybalance'] < 0) {
				$this->error ( '余额不足哦！' );
			}
			$pay ['paymoneybalance'] = $seller ['paybalance'];
			// 充值记录
			$payresult = $p->add ( $pay );
			
			if ($payresult) {
				// 更新卖家余额
				$sellerresult = $s->where ( "id={$seller['id']} AND nick='{$seller['nick']}'" )->save ( $seller );
				if ($sellerresult) {
					$s->commit ();
					$this->ajaxReturn ( $pay, '款项变更成功', 1 );
				} else {
					$s->rollback ();
					$this->error ( '款项变更失败' . $p->getError () . '-' . $s->getError () );
				}
			} else {
				$s->rollback ();
				$this->error ( $p->getError () . '-' . $s->getError () );
			}
		}
	}
	/**
	 * 收支记录
	 */
	function payment() {
		$id = intval ( $_GET ['id'] );
		$count = D( "Sellersetting" )->getPaymentCount( $id );
		
		import ( "ORG.Util.Page" ); // 导入分页类
		$p = new Page ( $count, $this->pagesize );
		
		$paylist = D ( "Sellersetting" )->getPayment ( $id, $p->firstRow , $p->listRows );
		$page = $p->show ();
		$this->assign ( "page", $page );
		
		$this->assign ( 'paylist', $paylist );
		$this->display ();
	}
	/**
	 * 发货人信息配置
	 */
	function sconfig() {
		$sconfig = F('sconfig');
		$this->assign('sellerattrs', $this->sellerattrs );
		$this->assign('sconfig', $sconfig );
		$this->display ();
	}
	
	function sconfigsave(){
		$vo = F('sconfig', $_POST);
		if( $vo ) {
		$this->ajaxReturn($vo, '更新成功', 1);
		}else {
			$this->error('error');
		}		
	}
}